<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { getInvoiceListApi, getInvoiceDetListApi } from "@/api/invoice"
import { type FormInstance } from "element-plus"
import { Search, Refresh, Download, RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
// import { useRouter } from "vue-router"

// const router = useRouter()

defineOptions({
  // 命名当前组件
  name: "logList"
})

const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const pageData = ref({
  dialogErrorMessageVisible: false,
  dialogDetPage: false,
  currentShowErrorMessage: ""
})

//#region 查
const tableData = ref<any>([])
const tableDetData = ref<any>([])
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  onlineCode: "",
  invoiceSn: ""
})
const getTableData = () => {
  loading.value = true
  getInvoiceListApi({
    startPage: paginationData.startPage,
    pageSize: paginationData.pageSize,
    ...searchData
  })
    .then((res) => {
      paginationData.total = res.count
      tableData.value = res.data
      console.log(tableData.value)
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  paginationData.startPage === 1 ? getTableData() : (paginationData.startPage = 1)
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}

const rowDbclick = (row: any) => {
  console.log(row)

  pageData.value.dialogDetPage = true

  loading.value = true
  getInvoiceDetListApi({
    startPage: 1,
    pageSize: 9999,
    internalInvoiceId: row.internalInvoiceId
  })
    .then((res) => {
      tableDetData.value = res.data
      console.log(tableData.value)
    })
    .catch(() => {
      tableDetData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}

//#endregion

/** 监听分页参数的变化 */
watch([() => paginationData.startPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="onlineCode" label="线上单号">
          <el-input v-model="searchData.onlineCode" placeholder="请输入" />
        </el-form-item>

        <el-form-item prop="invoiceSn" label="发票流水号">
          <el-input v-model="searchData.invoiceSn" placeholder="请输入" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-tooltip content="下载">
            <el-button type="primary" :icon="Download" circle />
          </el-tooltip>
          <el-tooltip content="刷新当前页">
            <el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData" @row-dblclick="rowDbclick">
          <el-table-column type="selection" width="50" align="center" />

          <el-table-column prop="invoiceSn" width="200" label="发票流水号" align="center" />
          <el-table-column prop="invoiceAmount" label="发票金额" align="center" />
          <el-table-column prop="taxAmount" label="去税" align="center" />
          <el-table-column prop="tax" label="税额" align="center" />
          <el-table-column prop="invoiceCode" width="130" label="发票代码" align="center" />
          <el-table-column prop="invoiceDate" width="130" label="发票日期" align="center" />
          <el-table-column prop="invoiceNo" width="180" label="发票号" align="center" />
          <!-- <el-table-column prop="invoiceType" label="发票类型" align="center" /> -->
          <el-table-column prop="invoiceType" width="80" label="发票类型" align="center">
            <template #default="scope">
              <el-tag v-if="scope.row.invoiceType == 1" type="success">蓝票</el-tag>
              <el-tag v-if="scope.row.invoiceType == 2" type="danger">红票</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="ifSpecialInvoice" width="90" label="专票/普票" align="center">
            <template #default="scope">
              <el-tag v-if="scope.row.ifSpecialInvoice == 0">普票</el-tag>
              <el-tag v-if="scope.row.ifSpecialInvoice == 1" type="success">专票</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="titleType" width="140" label="发票抬头类型" align="center" />
          <el-table-column prop="internalOrderCode" label="内部单号" align="center" />
          <el-table-column prop="shopCode" width="90" label="店铺编码" align="center" />
          <el-table-column prop="onlineCode" width="200" label="线上单号" align="center" />
          <el-table-column prop="remark" width="200" label="发票备注" align="center" />
          <el-table-column prop="jstCreateTime" width="200" label="聚水潭创建时间" align="center" />
          <el-table-column prop="jstModifiedTime" width="200" label="聚水潭修改时间" align="center" />
          <el-table-column prop="srcInternalInvoiceId" label="来源发票ID" align="center" />
          <el-table-column prop="internalInvoiceId" label="内部发票序列号" align="center" />
          <el-table-column prop="buyerName" width="200" label="发票人" align="center" />
          <el-table-column prop="buyerBank" width="200" label="开户行" align="center" />
          <el-table-column prop="buyerAddress" width="200" label="开票地址" align="center" />
          <el-table-column prop="buyerAccount" width="200" label="开户行账号" align="center" />
          <el-table-column prop="buyerPhoneZ" width="200" label="开户行账号" align="center" />
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.startPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <el-dialog v-model="pageData.dialogErrorMessageVisible" title="异常信息" width="70%">
      <div class="error-log">{{ pageData.currentShowErrorMessage }}</div>
    </el-dialog>

    <!-- 发票详情 -->
    <el-dialog v-model="pageData.dialogDetPage" title="发票明细" width="70%">
      <div class="table-wrapper">
        <el-table :data="tableDetData">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="materialCode" label="商品编码" align="center" />
          <el-table-column prop="materialName" width="500" label="商品名称" align="center" />
          <el-table-column prop="internalInvoiceDetId" width="180" label="内部发票明细序列号" align="center" />
          <el-table-column prop="internalInvoiceId" width="180" label="内部发票序列号" align="center" />
          <el-table-column prop="qty" width="100" label="商品数量" align="center" />
          <el-table-column prop="price" width="100" label="含税金额" align="center" />
          <el-table-column prop="taxRate" width="100" label="税率" align="center" />
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}

.keywordDiv {
  // max-width: 200px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: pointer;
}

.errorMsgdiv {
  // max-width: 200px;
  color: red;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: pointer;
}

.error-log {
  background-color: #333; /* 深灰背景 */
  color: #bada55; /* 亮绿色文字 */
  font-family: "Courier New", monospace; /* 等宽字体 */
  padding: 10px; /* 内边距 */
  border-radius: 5px; /* 边角圆滑 */
  border: 1px solid #666; /* 边框颜色稍浅 */
  white-space: pre-wrap; /* 保持换行和空格 */
  word-wrap: break-word; /* 长单词也可以断行 */
  // max-width: 600px; /* 最大宽度 */
  margin: 20px auto; /* 居中显示 */
}
</style>
